from loguru import logger
import commons
from commons import singleton


@singleton
class Configs:
    def __init__(self):
        self.configs = None
        self.get_configs_from_db()

    def get_configs_from_db(self):
        db = commons.get_db()
        cursor = db.cursor()
        cursor.execute("SELECT name, value FROM configs")
        self.configs = {row[0]: row[1] for row in cursor.fetchall()}
        self.configs["ema列表"] = [1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 13, 14, 15, 16, 21, 34, 55, 89, 233]
        self.configs["k线列表"] = ["1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "1d"]
        self.configs["交易对"] = "BTCFDUSD"
        db.close()
        return self.configs

    @classmethod
    def set_config_to_db(cls, key, value):
        db = commons.get_db()
        cursor = db.cursor()
        cursor.execute(f"UPDATE configs SET value='{value}' WHERE name='{key}';")
        db.commit()
        db.close()

    def get_configs(self):
        return self.configs


if __name__ == '__main__':
    configs = Configs()
    logger.debug(configs.get_configs())
    logger.debug(configs.get_configs_from_db())
    logger.debug(configs.get_configs())
